<?php

include_once('dao.connection.php');
include_once('entity.polygon.php');
include_once('entity.point.php');
include_once('entity.ring.php');


class PolygonDAO{

    private static $instance;

    public static function getInstance() {
        if (!isset(self::$instance))
            self::$instance = new PolygonDAO();

        return self::$instance;
    }

    private function __construct() {
        $this->connection = Connection::getInstance();
    }

    public function findById($id) {

        $sql = 'SELECT nm_polygon, (ST_Dump(polygon_geom)).geom as geom, ST_NumInteriorRings(polygon_geom) as num
            FROM polygon 
            WHERE id_polygon=' . $id;

        $result = $this->connection->executeSQL($sql);

        $polygon = new Polygon();

        while ($row = pg_fetch_object($result)) {
            $polygon->setId($id);            
            $polygon->setName($row->nm_polygon);

            $polygon->setExteriorRing($this->retrieveExteriorRing($row->geom));
            
            if ($row->num > 0) {
                for ($i = 1; $i <= $row->num; $i++) {
                    $rings[] = $this->retrieveInteriorRing($row->geom, $i);
                }
                $polygon->setInteriorRings($rings);
            }
        }
        return $polygon;
    }

    public function listPolygons() {

        $sql = 'SELECT * FROM polygon';
        $result = $this->connection->executeSQL($sql);
        
        while ($row = pg_fetch_object($result)) {
            $polygon = new Polygon();
            $polygon->setId($row->id_polygon);
            $polygon->setName($row->nm_polygon);

            $polygons[]= $polygon;
        }
        return $polygons;
    }

    public function retrieveExteriorRing($geom) {

        $sql = "SELECT x(ST_AsText(geom)), y(ST_AsText(geom))
                FROM (
                  SELECT (ST_DumpPoints(g.geom)).* 
                  FROM
                    (SELECT ST_AsText(ST_ExteriorRing('".$geom."'))::geometry AS geom) AS g
                  ) j";

        $result = $this->connection->executeSQL($sql);

        while (list($x, $y) = pg_fetch_row($result)) {
            $point = new Point();
            $point->setX($x);
            $point->setY($y);
            $points[] = $point;
        }
        $ring = new Ring();
        $ring->setPoints($points);
        return $ring;
    }
        
    public function retrieveInteriorRing($geom, $ringN) {

        $sql = "SELECT x(ST_AsText(geom)), y(ST_AsText(geom))
                FROM (
                  SELECT (ST_DumpPoints(g.geom)).* 
                  FROM
                    (SELECT ST_AsText(ST_InteriorRingN('".$geom."',". $ringN ."))::geometry AS geom) AS g
                  ) j";
        
        $result = $this->connection->executeSQL($sql);

        while ($row = pg_fetch_object($result)) {
            $point = new Point();
            $point->setX($row->x);
            $point->setY($row->y);
            $points[] = $point;
        }
        $ring = new Ring();
        $ring->setPoints($points);

        return $ring;
    }

    
 
    
}

?>